// ==UserScript==
// @name          CHP Highlight
// @namespace     http://wtwf.com/
// @description	  Adds a way to popup stuff on select boxes
// @include       http://cad.chp.ca.gov*

// Copyright 2007 wtwf.com, All Rights Reserved.
// Created by: Alex K (wtwf.com)

var RCS_REVISION = "$Revision: 1.6 $";

// ==/UserScript==

function chp_hihglight() {

// Add words here you're interested in (ALL LOWER CASE!)
const WORDS = ["sr85", "fatality", "sr17", "nb17", "sb17",
               "laurel", "spanish oaks", "summit"];

// Add names of areas in here you're interested in
const AREAS = ["San Jose", "Santa Cruz"];

const RULES = new Array(
    ".TB {font-size: 14; font-family: Arial; font-weight: bold;}"
    );

 /** is this row in the table interesting */
function interestingRow(row) {
  for (var k = 0; k < WORDS.length ; k++) {
    if (row.nodeValue &&
        row.nodeValue.toLowerCase().indexOf(WORDS[k]) != -1){
      return true;
    }
  }
  if (row.childNodes) {
    var cells = row.childNodes;
    for (var i = 0; i < cells.length; i++) {
      if (interestingRow(cells[i])) {
        return true;
      }
    }
  }
  return false;
}

/** highlight every cell in this table row (by setting a style class) */
function highlightRow(row) {
  initializeStyles();
  var cells = row.childNodes;
  for (var i = 0; i < cells.length; i++) {
    cells[i].className = "TB";
  }
}

/** Work out if any rows on this page are interesting and highlight
 * them if they are. */
function hightlightInteresting(input) {
  // We're only interested in the frame called 'sa'
  if (window.name != "sa") {
    return;
  }
  var ele = document.getElementsByTagName("tr");

  if (ele.length > 0) {
    lastgood = false;
    bad = Array();
    for (var i = 0; i < ele.length; i++) {
      if (interestingRow(ele[i])){
        highlightRow(ele[i]);
        lastgood = ele[i].parentNode;
      } else {
        if (ele[i].firstChild && ele[i].firstChild.className == "T"){
          bad.push(ele[i]);
        }
      }
    }
    if (lastgood){
      // move all the good ones to the top (or the bad ones to the bottom)
      for (var i = 0; i < bad.length; i++) {
        bad[i].parentNode.removeChild(bad[i]);
        lastgood.appendChild(bad[i]);
      }
    }
  }
}


/** make sure we have the style sheet we're going to use (only
 * inserted if we need it */
function initializeStyles() {
  if (stylesInited) {
    return;
  }
  stylesInited = true;
  var styleNode = document.createElement("style");

  document.body.appendChild(styleNode);

  styleSheet = document.styleSheets[document.styleSheets.length - 1];

  for (var i = 0; i < RULES.length; i++) {
    styleSheet.insertRule(RULES[i], 0);
  }
}

/** pre fill out the custom region defining form from values in AREAS */
function selectCustomRegion() {
  var eles = document.getElementsByTagName("SELECT");

  for (var i = 0; i < eles.length; i++) {
    if (eles[i].name == "centerin" && eles[i].multiple == true) {
      selectCustomRegonSelect(eles[i]);
    }
  }
}

/** we've found a <select> we wangt to fill in with selected regions */
function selectCustomRegonSelect(ele) {
  for (var i = 0; i < ele.options.length; i++ ) {
    for (var j = 0; j < AREAS.length; j++ ) {
      if (ele.options[i].text.indexOf(AREAS[j]) != -1) {
        ele.options[i].selected = true;
      }
    }
  }
}

var stylesInited = false;
hightlightInteresting();
selectCustomRegion();
}

window.setTimeout(chp_hihglight, 500);

